<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>动态综合演示 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d,mars3d-space"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      'use script' //开发环境建议开启严格模式

      var map

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: {
              y: -12.46482,
              x: 115.555352,
              z: 7005441.2,
              heading: 356.8,
              pitch: -78.4,
              roll: 0,
            },
            cameraController: {
              zoomFactor: 3.0,
              minimumZoomDistance: 1000,
              maximumZoomDistance: 300000000,
              constrainedAxis: false, //解除在南北极区域鼠标操作限制
            },
            clock: {
              multiplier: 5, //速度
            },
          },
          control: {
            animation: true, //是否创建动画小器件，左下角仪表
            timeline: true, //是否显示时间线控件
          },
        })

        //创建三维地球场景
        map = new mars3d.Map('mars3dContainer', mapOptions)

        //移除2.5D视图
        var arr = $('.cesium-sceneModePicker-wrapper').children()
        if (arr.length > 3) {
          arr[3].remove()
        }

        //创建矢量数据图层
        var graphicLayer = new mars3d.layer.GraphicLayer()
        map.addLayer(graphicLayer)

        haoutil.msg('非实际卫星轨道，随机模拟的坐标，只是为了演示追踪！')

        //===========================================================
        //取数据
        var property = getDynamicProperty(function (timeInterval) {
          graphic.entity.availability = new Cesium.TimeIntervalCollection([new Cesium.TimeInterval(timeInterval)])
        })

        var times = property._property._times
        var startTime = times[0].clone()
        var stopTime = times[times.length - 1].clone()

        var graphic = new mars3d.graphic.PathEntity({
          availability: new Cesium.TimeIntervalCollection([
            new Cesium.TimeInterval({
              start: startTime,
              stop: stopTime,
            }),
          ]),
          position: property, // 点集
          orientation: new Cesium.VelocityOrientationProperty(property),
          style: {
            leadTime: 0,
            trailTime: 2800,
            resolution: 1,
            material: mars3d.MaterialUtil.createMaterialProperty(mars3d.MaterialType.PolylineGlow, {
              glowPower: 0.1,
              color: Cesium.Color.YELLOW,
            }),
            width: 10,
          },
          model: {
            url: 'http://data.marsgis.cn/gltf/mars/weixin.gltf',
            scale: 1,
            minimumPixelSize: 150,
          },
        })
        graphicLayer.addGraphic(graphic)

        //===========================================================

        //视锥体 展示
        var satelliteSensor = new mars3d.graphic.SatelliteSensor({
          position: property,
          autoHeading: true, //是否自动角度，根据监听的卫星模型
          style: {
            sensorType: mars3d.graphic.SatelliteSensor.Type.Rect,
            angle1: 20,
            angle2: 10,
            heading: 0,
            pitch: 0,
            roll: 0,
            color: 'rgba(110,245,0,0.5)',
          },
        })
        graphicLayer.addGraphic(satelliteSensor)

        satelliteSensor.trackedEntity = graphic //本身跟随卫星

        //地面站 展示
        var localStart = Cesium.Cartesian3.fromDegrees(109.51856, 18.258736, 2)
        var conicSensor = new mars3d.graphic.ConicSensor({
          position: localStart,
          style: {
            angle: 5, //雷达最小扫描仰角
            length: 2500000,
            color: Cesium.Color.fromBytes(255, 0, 0, 85),
          },
        })
        graphicLayer.addGraphic(conicSensor)

        conicSensor.lookAt = property //追踪卫星

        //测试连接线
        let testLine = new mars3d.graphic.PolylineEntity({
          positions: new Cesium.CallbackProperty(function (time) {
            var localEnd = conicSensor.rayPosition
            if (!localEnd) {
              return []
            }
            return [localStart, localEnd]
          }, false),
          style: {
            arcType: Cesium.ArcType.NONE,
            material: mars3d.MaterialUtil.createMaterialProperty(mars3d.MaterialType.PolylineDash, {
              color: '#ff0000',
            }),
            width: 1,
          },
        })
        graphicLayer.addGraphic(testLine)
      }

      //构造模拟数据，实际项目应改为服务读取返回
      function getDynamicProperty(callback) {
        var arr = dataWork.getTestData(Cesium.JulianDate.toIso8601(map.clock.currentTime), 2 * 60)

        var property = new Cesium.SampledPositionProperty()
        for (var i = 0; i < arr.length; i++) {
          var item = arr[i]

          var thisTime = Cesium.JulianDate.fromIso8601(item.time)
          var position = Cesium.Cartesian3.fromDegrees(item.x, item.y, item.z)

          // 添加每一个链接点的信息，到达的时间以及坐标位置
          property.addSample(thisTime, position)
        }

        var times = property._property._times
        var startTime = times[0].clone()
        var stopTime = times[times.length - 1].clone()

        var allTimes = Cesium.JulianDate.secondsDifference(stopTime, map.clock.currentTime) * 0.3 //判断剩下多少时长时加载下一步数据
        var loading = false
        map.on(mars3d.EventType.clockTick, function (clock) {
          var sxTimes = Cesium.JulianDate.secondsDifference(stopTime, map.clock.currentTime) //剩下时长

          if (!loading && sxTimes < allTimes) {
            loading = true
            var arr = dataWork.getTestData(Cesium.JulianDate.toIso8601(stopTime), 2 * 60)

            //console.log('new loading……');
            for (var i = 1; i < arr.length; i++) {
              var item = arr[i]

              var thisTime = Cesium.JulianDate.fromIso8601(item.time)
              var position = Cesium.Cartesian3.fromDegrees(item.x, item.y, item.z)

              //console.log(JSON.stringify(item));

              // 添加每一个链接点的信息，到达的时间以及坐标位置
              property.addSample(thisTime, position)
            }

            var times = property._property._times
            stopTime = times[times.length - 1].clone()

            loading = false
            if (callback) {
              callback({
                start: startTime,
                stop: stopTime,
              })
            }
          }
        })

        return property
      }

      //模拟数据生产类
      var dataWork = {
        thisPoint: {
          x: 100.245989,
          y: 0,
          z: 1000000,
        },
        //data开始时间，seconds 秒数
        getTestData: function (date, seconds) {
          var startTime = Cesium.JulianDate.fromIso8601(date) //飞行开始时间

          var arr = []

          var thisTime
          for (var i = 0; i <= seconds; i += 5) {
            thisTime = Cesium.JulianDate.addSeconds(startTime, i, new Cesium.JulianDate())

            // 生成随机的坐标
            this.thisPoint.x += i * 0.01
            this.thisPoint.y += i * 0.01

            arr.push({
              time: Cesium.JulianDate.toIso8601(thisTime),
              x: this.thisPoint.x,
              y: this.thisPoint.y,
              z: this.thisPoint.z,
            })
          }
          return arr
        },
      }
    </script>
  </body>
</html>
